package com.byx.chatclient.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Base64;
import android.widget.ImageView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * SharePerfencesUtil
 *
 * @author Superb
 * @date 2021/4/3 23:05
 */
public class SharedPreferencesUtil {
    private static SharedPreferences sSharedPreferences;
    private static WeakReference<Context> mContext;

    public void bindContext(Context context){
        mContext.clear();
        mContext = new WeakReference<>(context);
    }

    private static void save(SharedPreferences.Editor editor) {
        editor.apply();
    }

    public static void writeImage(String fileName, String key, ImageView imageView) {
        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        // 将Bitmap压缩成字节数组输出流
        ByteArrayOutputStream byStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byStream);
        // 利用Base64将我们的字节数组输出流转换成String
        byte[] byteArray = byStream.toByteArray();
        String imgString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
        // 将String保存shareUtils
        write(fileName, key, imgString);
    }

    public static void write(String fileName, String k, int v) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(k, v);
        save(editor);
    }

    public static void write(String fileName, String k, long v) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(k, v);
        save(editor);
    }

    public static void write(String fileName, String k, boolean v) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(k, v);
        save(editor);
    }

    public static void write(String fileName, String k, String v) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(k, v);
        save(editor);
    }

    public static Bitmap readImage(String fileName, String key, ImageView imageView) {
        String imgString = readString(fileName, key, "");
        if (!"".equals(imgString)) {
            // 利用Base64将我们string转换
            byte[] byteArray = Base64.decode(imgString, Base64.DEFAULT);
            ByteArrayInputStream byStream = new ByteArrayInputStream(byteArray);
            // 生成bitmap
            return BitmapFactory.decodeStream(byStream);
        }
        return null;
    }

    public static int readInt(String fileName, String k) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getInt(k, 0);
    }

    public static int readInt(String fileName, String k, int defv) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getInt(k, defv);
    }

    public static long readLong(String fileName, String k,
                                long defv) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getLong(k, defv);
    }

    public static boolean readBoolean(String fileName, String k) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getBoolean(k, false);
    }

    public static boolean readBoolean(String fileName, String k, boolean defBool) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getBoolean(k, defBool);
    }

    public static String readString(String fileName, String k) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getString(k, null);
    }

    public static String readString(String fileName, String k, String defV) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return preferences.getString(k, defV);
    }

    public static Map<String, ?> readAll(String fileName) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        Map<String, ?> map = preferences.getAll();
        return map;
    }

    public static void remove(String fileName, String k) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(k);
        save(editor);
    }

    public static void clean(String fileName) {
        SharedPreferences preferences = mContext.get().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        save(editor);
    }
}
